import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import { Models } from '@/common/typings/modle'
import { HttpException, ParameterException, Success } from '@/core/HttpException'
import { command } from '@/server/mysql'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/valueSet`,
})
router.post('/delBySetCode', verifyToken, async (ctx: Models.Ctx) => {
  const { setCode, ids } = ctx.request.body as any
  if (!(ids || setCode)) {
    throw new ParameterException()
  }
  let where = `WHERE ${ids ? `set_code IN (${ids.map((i: string) => `'${i}'`).join()})` : `set_code = '${setCode}'`}`
  const res = (await command(`DELETE FROM system_value_set ${where};`)).results
  if (res.fieldCount === 0) {
    throw new Success({ msg: '删除成功' })
  }
  throw new HttpException({ msg: '删除时遇到问题：' + JSON.stringify(res) })
})
export default router
